import { defineStore } from "pinia";


export const useCartStore=defineStore('cart',{
    state:()=>({
        items:[]
    }),
    actions:{
        // 添加
        addItem(item){
            const mall=this.items.find(i =>i.id === item.id)
            if(mall){
                mall.quantity +=item.quantity
            }else{
                this.items.push(item)
            }
        },
        // 移除
        removeItem(itemId){
           this.items = this.items.filter(item=>item.id !==itemId)
        },
        // 清空
        clearCart(){
            this.items=[]
        }
    },
    persist:true//开启持久化
})